Imports System.Web.Security
Imports MIMS.WRS.Business
Imports MIMS.WRS.Data

Public Class Login
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Protected WithEvents txtUsername As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
    Protected WithEvents imgBtnLogin As System.Web.UI.WebControls.ImageButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        

        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            Dim logout As Boolean
            If Not IsNothing(Request.QueryString("logout")) Then
                logout = CBool(Request.QueryString("logout"))
                If logout Then
                    Session.Clear()
                    FormsAuthentication.SignOut()
                End If
            End If

            ' check to see request page is a dialog
            Dim url As String
            url = Request.QueryString("ReturnUrl")

            If Not IsNothing(url) Then
                Dim ss() As String = url.Split("/"c)
                If ss(ss.Length - 2) = "Dialogs" Then
                    Response.Redirect(Config.AppRoot & "/Errors/SessionExpired.aspx?source=1")
                End If
            End If
        End If
    End Sub


    Private Sub imgBtnLogin_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgBtnLogin.Click
        Try
            '            Session.Clear()
            FormsAuthentication.SignOut()
            Dim userID As Integer
            userID = USERService.Login(txtUsername.Text.Trim(), Utilities.HashPassword(txtPassword.Text))

            RequestContext.CurrentUser = USERService.GetUSER(userID)

            Session(Security.CURRENT_USER_SESSION_NAME) = RequestContext.CurrentUser

            Session("CurrentUserID") = userID

            If RequestContext.CurrentUser.USER_TYPE = MIMS.WRS.Entities.UserType.Customer _
               Or RequestContext.CurrentUser.USER_TYPE = MIMS.WRS.Entities.UserType.Sales Then
                Throw New Exception("Sorry, you are not allowed here!")
            End If

            RequestContext.CurrentOffice = OfficeService.GetOffice(RequestContext.CurrentUser.ActiveOfficeID)

            FormsAuthentication.SetAuthCookie(RequestContext.CurrentUser.NAME, False)

            Dim url As String
            url = Request.QueryString("ReturnUrl")

            If url <> Nothing Then
                url = Server.UrlDecode(url)
                Response.Redirect(url)
            Else
                Response.Redirect(Config.AppRoot & "/StartPage.aspx")
            End If

        Catch ex As Exception
            lblErr.Text = ex.Message
        End Try
    End Sub
End Class
